﻿namespace MicroCloud.EventBuses
{
    /// <summary>
    /// 事件总线构建器
    /// </summary>
    internal class EventBusBuilder : IEventBusBuilder
    {
        /// <summary>
        /// 事件总线接口
        /// </summary>
        private readonly IEventBus _eventBus;

        /// <summary>
        /// 初始化一个事件总线构建器 <see cref="EventBusBuilder"/> 的新实例
        /// </summary>
        /// <param name="eventBus"></param>
        public EventBusBuilder(IEventBus eventBus)
        {
            _eventBus = eventBus;
        }

        #region IEventBusBuilder
        /// <summary>
        /// 构建事件总线
        /// </summary>
        public void Build()
        {
            Type[] types = AssemblyManager.FindTypesByBase(typeof(IEventHandler<>));
            if (types.Length == 0)
            {
                return;
            }
            _eventBus.SubscribeAll(types);
        }

        #endregion

    }

}
